package com.sky.controller.admin;

import com.sky.anno.AutoFill;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@Api(tags = "通用接口")
@RequestMapping("/admin/common")
@RestController
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;
    /**
     * 文件上传
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传: {}",file);
        //1. 获取源文件名
        String originalFilename = file.getOriginalFilename();
        //2. 截取源文件名称中的后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //3. 生成随机文件名, 并拼接后缀
        String fileName = UUID.randomUUID() + suffix;
        //4. 调用工具类的文件上传的方法, 将文件上传到aliyun,返回图片的访问路径
        String url = aliOssUtil.upload(file.getBytes(), fileName);
        //5. 返回图片的访问路径
        return Result.success(url);
    }
}
